#include "stdafx.h"
#include <direct.h>

bool g_debug=false;
int g_offset=0;
int g_startY=0;
bool g_errorHappened=false;
bool g_rotate=false;
CvPoint g_ROI, g_codeblock, g_parts;
CvRect g_imageCoord;
std::string g_datadir, g_imagedir;
std::string g_path;
std::string g_filenameAppend;
CvPoint g_topleft=cvPoint(0,0), g_lowerright=cvPoint(0,0);
int g_yParts[4];


//************************************
// Method:    	init
// FullName:  	Init::init
// Access:    	public static 
// Returns:   	void
// Parameter: 	string parFileName
// Parameter: 	int type
// Description:	reads values from config.txt for later use
//************************************
void Init::init(string parFileName)
{
	Debug::message(">>>  Init\t\t\t<<<",Debug::VERBOSE);
	
	ParFile *parfile = new ParFile(parFileName.c_str());

	if(parfile->GetValue("CONFIG","DEBUG") != NULL && atoi(parfile->GetValue("CONFIG","DEBUG"))<5)
	{
		int level = atoi(parfile->GetValue("CONFIG","DEBUG"));
		if (level == 0)
		{
			g_debug=false;
			Debug::setQuiet();
		} else if (level == 1)
		{
			g_debug=true;
			Debug::setLevel(0);
		}
	}
	else
		cout<<"Debug disabled"<<endl;

	//Debug::message(">>>Init<<<",Debug::VERBOSE);

	if(parfile->GetValue("CONFIG","PARTSX")!=NULL && parfile->GetValue("CONFIG","PARTSY")!=NULL)
	{
		g_parts.x=atoi(parfile->GetValue("CONFIG","PARTSX"));
		g_parts.y=atoi(parfile->GetValue("CONFIG","PARTSY"));
	}
	

	if(parfile->GetValue("CONFIG","WIDTH")!=NULL && parfile->GetValue("CONFIG","HEIGHT")!=NULL)
	{
		g_ROI.x=atoi(parfile->GetValue("CONFIG","WIDTH"));
		g_ROI.y=atoi(parfile->GetValue("CONFIG","HEIGHT"));
	}

	if(parfile->GetValue("CODEBLOCK","WIDTH")!=NULL && parfile->GetValue("CODEBLOCK","HEIGHT")!=NULL)
	{
		g_codeblock.x=atoi(parfile->GetValue("CODEBLOCK","WIDTH"));
		g_codeblock.y=atoi(parfile->GetValue("CODEBLOCK","HEIGHT"));
	}

	/*
	if (type == 0)
		{
			if(parfile->GetValue("TELELENS","IMAGEX")!=NULL && parfile->GetValue("TELELENS","IMAGEY")!=NULL &&
				parfile->GetValue("TELELENS","OFFSETX")!=NULL && parfile->GetValue("TELELENS","OFFSETY")!=NULL)
			{
				g_imageCoord=cvRect(atoi(parfile->GetValue("TELELENS","OFFSETX")),atoi(parfile->GetValue("TELELENS","OFFSETY")),
					atoi(parfile->GetValue("TELELENS","IMAGEX")), atoi(parfile->GetValue("TELELENS","IMAGEY")));
			}
		} else {
			if(parfile->GetValue("TRILENS","IMAGEX")!=NULL && parfile->GetValue("TRILENS","IMAGEY")!=NULL &&
				parfile->GetValue("TRILENS","OFFSETX")!=NULL && parfile->GetValue("TRILENS","OFFSETY")!=NULL)
			{
				g_imageCoord=cvRect(atoi(parfile->GetValue("TRILENS","OFFSETX")),atoi(parfile->GetValue("TRILENS","OFFSETY")),
					atoi(parfile->GetValue("TRILENS","IMAGEX")), atoi(parfile->GetValue("TRILENS","IMAGEY")));
			}
		}*/
	

	if (parfile->GetValue("CONFIG","DATA")!=NULL)
	{
		ofstream data;
		std::string temp = parfile->GetValue("CONFIG","DATA");
		for (int i=0; i<strlen(temp.c_str())-1; i++)
			g_datadir+=temp[i];
		std::string path = g_path;
		path+=g_datadir;
		_mkdir(path.c_str());
		Init::initOutputFile();
	}
	

	if (parfile->GetValue("CONFIG","IMAGE")!=NULL)
	{
		std::string temp = parfile->GetValue("CONFIG","IMAGE");
		for (int i=0; i<strlen(temp.c_str())-1; i++)
			g_imagedir+=temp[i];
		std::string path = g_path;
		path+=g_imagedir;
		_mkdir(path.c_str());
	}
}

void Init::initOutputFile()
{
	std::string st(Init::getPath());
	ofstream output;


	//replace "\" with "/"
	for (int i=0; i<st.size(); i++)
	{
		if (st[i]=='\\')
			st[i]='/';
	}
	//appending all the stuff for a valid path
	st+=Init::getDataDir();
	st+="/data.csv";
	output.open(st.c_str(),ios::out);
	output<<"filename,time,lattitude,longitude,distance,altitude,heading,drift,roll,pitch,speed,date,talkingUnit,lensPos,event,senID"<<endl;
	output.close();
}

void Init::initErrorFile()
{
	std::string st(Init::getPath());
	ofstream output;


	//replace "\" with "/"
	for (int i=0; i<st.size(); i++)
	{
		if (st[i]=='\\')
			st[i]='/';
	}
	//appending all the stuff for a valid path
	st+=Init::getDataDir();
	st+="/error.csv";
	output.open(st.c_str(),ios::out);
	output<<"filename,part,error,timestamp"<<endl;
	output.close();
	g_errorHappened=true;
}

bool Init::getErrorHappened()
{
	return g_errorHappened;
}

CvRect Init::getImageCoord()
{
	return g_imageCoord;
}

bool Init::getDebug()
{
	return g_debug;
}

void Init::setDebug(bool level)
{
	g_debug=level;
	//level ? Debug::setLevel(0) : Debug::setQuiet();
}

std::string Init::getDataDir()
{
	return g_datadir;
}

std::string Init::getImageDir()
{
	return g_imagedir;
}

CvPoint Init::getParts()
{
	return g_parts;
}

CvPoint Init::getROI()
{
	return g_ROI;
}

CvPoint Init::getCodeblock()
{
	return g_codeblock;
}

void Init::setPath(std::string path)
{
	g_path=path;
}

std::string Init::getPath()
{
	return g_path;
}

void Init::setTopleft(int x, int y)
{
	if (x != -1)
		g_topleft.x=x;
	if (y != -1)
		g_topleft.y=y;
}

void Init::setLowerright(int x, int y)
{
	if (x != -1)
		g_lowerright.x = x;
	if (y != -1)
		g_lowerright.y = y;
}

void Init::setYParts(int part, int value)
{
	assert (part < 4 && part >=0);
	g_yParts[part]= value;
}

CvPoint Init::getTopleft()
{
	return g_topleft;
}

CvPoint Init::getLowerright()
{
	return g_lowerright;
}

int Init::getYPart(int part)
{
	assert (part <4 && part >= 0);
	return g_yParts[part];
}

void Init::setOffset(int x)
{
	assert (x>=0);
	g_offset = x;
}

int Init::getOffset()
{
	return g_offset;
}

void Init::setRotateImage(bool rotate)
{
	assert (rotate==true || rotate==false);
	g_rotate = rotate;
}

bool Init::getRotateImage()
{
	return g_rotate;
}

void Init::setFilenameAppend(std::string append)
{
	g_filenameAppend=append;
}

std::string Init::getFilenameAppend()
{
	return g_filenameAppend;
}